package rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeoutException;

public class ProducerDemo {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory=new ConnectionFactory();
        //1.设置IP地址
        connectionFactory.setHost("8.134.130.95");
        //2.设置端口号
        connectionFactory.setPort(5672);
        //3.设置虚拟机
        connectionFactory.setVirtualHost("bite");
        //4.设置用户名和密码
        connectionFactory.setUsername("study");
        connectionFactory.setPassword("study");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        //5.声明队列
        /**
         * queueDeclare
         * (
         * String queue,
         * boolean durable,
         * boolean exclusive,
         * boolean autoDelete,
         * Map<String, Object> arguments
         * )
         * queue:队列名称
         * durable：是否持久化，也就是是否将消息罗盘
         * exclusive：是否独占，也就是只能有一个消费者订阅这个队列
         * autoDelete：队列没有消费者时，队列是否自动删除
         */
        channel.queueDeclare("hello",true,false,false,null);
        //6.发送虚拟机
        /**
         *  basicPublish
         *  (String exchange, String routingKey, boolean mandatory, boolean immediate, BasicProperties props, byte[] body)
         *  exchange:交换机的名字，内置的交换机的名字为:""
         *  routingKey:路由标识，队列名称
         *  props:配置信息
         *  body:消息主题
         */
        String msg="hello rabbitmq~";
        channel.basicPublish("","hello",null,msg.getBytes(StandardCharsets.UTF_8));
        channel.close();
        connection.close();
    }
}
